# Maintainer: Anatol Pomozov <anatol dot pomozov at gmail>
# Contributor: Sven-Hendrik Haase <svenstaro@gmail.com>
# Contributor: Pavol (Lopo) Hluchy <lopo AT losys DOT eu>
# Contributor: Massimiliano Torromeo <massimiliano.torromeo@gmail.com>
# Contributor: Jonas Heinrich <onny@project-insnaity.org>
# Contributor: Lee Watson <aur@revthefox.co.uk>
# Contributor: Tobias Hunger <tobias DOT hunger AT gmail DOT com>
# Contributor: Stefan Tatschner <stefan@sevenbyte.org>


# NOTE: Gitlab isn't always compatible with modern Ruby versions. In that case, check the
# commit log for an old fix on how to tell it to use older versions of Ruby. I'm afraid we'll
# need this again at some point in the future.
pkgname=gitlab-shell
pkgver=14.18.0
pkgrel=1
epoch=3
pkgdesc="Git management software"
arch=('loong64' 'x86_64')
url="https://gitlab.com/gitlab-org/gitlab-shell/"
license=('MIT')
depends=('ruby' 'redis' 'git')
makedepends=('go')
options=('!strip' '!lto')
install=gitlab-shell.install
backup=("etc/webapps/${pkgname}/config.yml"
        "etc/webapps/${pkgname}/secret"
        "etc/webapps/${pkgname}/.gitconfig")
source=("${pkgname}-${pkgver}.tar.gz::https://gitlab.com/gitlab-org/gitlab-shell/-/archive/v${pkgver}/gitlab-shell-v${pkgver}.tar.gz"
        "configs.patch"
        "gitlab-shell.gitconfig"
        "gitlab-shell.sysusers")
sha512sums=('bbd929d0e7378f0d8e0bd5ef77a856ad5a971797956a7188d7947ebf1238533f6a9b66484dd65cc03fc0b9c0018d2a353883a359f902ace1f96def2e697840c2'
            '13c0fb88693e787f2960059985713d7ffb6d2fa51d9dbcba9c9562ae91e74bd3963efc63837cd19c5441fb5835d0c8eafd3285bea6676168f7b111a08fb086a7'
            '5c4892baa8224fdd1ddb679bd6036e281b9c896a8ba5764fb83d95fc01a45235bdacb2d870f946b13f7ffb47f6e6085f49e7473579fc36b242d19d0ce6c83981'
            '616188911fa7b7c569bc2d5e2024177e67366c48b4ffef1f7ad4dc8e4e9e2cdb421b687f45a1e9bdec8bf2736a157ca9f1e0ba24edae2f073b8c7b5ac42745e8')

_datadir="/usr/share/webapps/${pkgname}"
_etcdir="/etc/webapps/${pkgname}"
_homedir="/var/lib/gitlab"
_logdir="/var/log/gitlab"
_srcdir="gitlab-shell-v${pkgver}"

prepare() {
  cd ${_srcdir}

  patch -p1 < ../configs.patch
  # At this point config file should not contain any references to '/home/git'
git clone https://github.com/golang/sys.git
}

build() {
  cd ${_srcdir}

  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export CGO_LDFLAGS="${LDFLAGS}"
  export GOFLAGS="-trimpath -mod=readonly -modcacherw"
export GOPATH="$srcdir/build:/usr/share/gocode"
go mod tidy
go mod edit -replace=golang.org/x/sys@v0.0.0-20210412220455-f1c623a9e750=$srcdir/sys
  make build
}

package() {
  cd ${_srcdir}

  install -dm755 -o root -g root "${pkgdir}${_datadir}"
  install -dm750 -o 105 -g 105 "${pkgdir}${_homedir}"
  install -dm750 -o 105 -g 105 "${pkgdir}${_homedir}/satellites"
  install -dm700 -o 105 -g 105 "${pkgdir}${_homedir}/.ssh"
  install -dm750 -o 105 -g 105 "${pkgdir}${_etcdir}"
  install -dm770 -o 105 -g root "${pkgdir}${_logdir}"

  # sysusers
  install -Dm644 "${srcdir}/gitlab-shell.sysusers" "${pkgdir}/usr/lib/sysusers.d/gitlab-shell.conf"

  # .gitconfig
  install -Dm644 -o 105 -g 105 "${srcdir}/gitlab-shell.gitconfig" "${pkgdir}${_etcdir}/.gitconfig"
  ln -fs "${_etcdir}/.gitconfig" "${pkgdir}${_homedir}/.gitconfig"

  # .gitlab_shell_secret
  touch "${pkgdir}${_etcdir}/secret"
  chmod 640 "${pkgdir}${_etcdir}/secret"
  chown root:105 "${pkgdir}${_etcdir}/secret"
  ln -fs "${_etcdir}/secret" "${pkgdir}${_datadir}/.gitlab_shell_secret"

  # config.yml
  install -Dm644 config.yml.example "${pkgdir}${_etcdir}/config.yml"
  ln -fs "${_etcdir}/config.yml" "${pkgdir}${_datadir}/config.yml"

  cp -r VERSION bin spec support "${pkgdir}${_datadir}"
  ln -fs "${_datadir}" "${pkgdir}/${_homedir}/"

  install -dm2770 -o 105 -g 105 "${pkgdir}${_homedir}/repositories"

  install -Dm644 LICENSE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
  install -Dm644 README.md "${pkgdir}/usr/share/doc/${pkgname}/README.md"
}

# vim:set ts=2 sw=2 et:
